﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace BlueLotus7.SSQAnalysisCore
{
    public class BallCollection : ICloneable
    {
        public decimal[] Balls
        {
            get;
            set;
        }

        #region Properties

        public decimal this[int number]
        {
            get
            {
                return GetValueByNumber(number);
            }
            set
            {
                SetValueByNumber(number, value);
            }
        }

        public int Count
        {
            get
            {
                return this.Balls.Length;
            }
        }

        #endregion

        public BallCollection(int count)
        {
            this.Balls = new decimal[count];
        }

        public decimal GetValueByNumber(int number)
        {
            if (number > 0 && number <= Balls.Length)
            {
                return Balls[number - 1];
            }

            return 0;
        }

        public void SetValueByNumber(int number, decimal value)
        {
            if (number > 0 && number <= Balls.Length)
            {
                Balls[number - 1] = value;
            }
        }

        public void IncreaseValue(int number, decimal value)
        {
            if (number > 0 && number <= Balls.Length)
            {
                Balls[number - 1] += value;
            }
        }

        public void Clear()
        {
            Array.Clear(this.Balls, 0, this.Balls.Length);
        }

        /// <summary>
        /// Gets red ball list sorted by value DESC
        /// </summary>
        /// <returns></returns>
        public List<KeyValuePair<int, decimal>> GetBallSortedList()
        {
            return GetSortedList(this.Balls);
        }

        /// <summary>
        /// Gets list sorted by value DESC
        /// </summary>
        /// <returns></returns>
        protected static List<KeyValuePair<int, decimal>> GetSortedList(decimal[] sources)
        {
            List<KeyValuePair<int, decimal>> result = new List<KeyValuePair<int, decimal>>();

            if (sources != null)
            {
                for (int i = 0; i < sources.Length; i++)
                {
                    result.Add(new KeyValuePair<int, decimal>(i + 1, sources[i]));
                }

                SortPercentageDESC(result);
            }

            return result;
        }

        /// <summary>
        /// Extend SortPercentageDESC for <c>List&lt;KeyValuePair&lt;int, decimal&gt;&gt;</c>.
        /// </summary>
        /// <param name="items"></param>
        protected static void SortPercentageDESC(List<KeyValuePair<int, decimal>> items)
        {
            if (items != null)
            {
                items.Sort(SortPercentageDESC);
            }
        }

        /// <summary>
        /// Implement IComparison interface for <c>KeyValuePair&lt;int, decimal&gt;</c>
        /// </summary>
        /// <param name="item1"></param>
        /// <param name="item2"></param>
        /// <returns></returns>
        protected static int SortPercentageDESC(KeyValuePair<int, decimal> item1, KeyValuePair<int, decimal> item2)
        {
            return -1 * item1.Value.CompareTo(item2.Value);
        }

        /// <summary>
        /// Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns> A new object that is a copy of this instance.</returns>
        public object Clone()
        {
            BallCollection cloneObject = new BallCollection(this.Count);
            Array.Copy(this.Balls, cloneObject.Balls, this.Count);

            return cloneObject;
        }
    }
}
